<?php

/**
 * UserIdentity represents the data needed to identity a user.
 * It contains the authentication method that checks if the provided
 * data can identity the user.
 */
class UserIdentity extends CUserIdentity
{

    private $_id;
    private $_needen;
    private $_mobile;
    private $_firstLogin;

    public function __construct($username, $password, $needen = true,$_firstLogin = false)
    {
        parent::__construct(trim($username), trim($password));
        $this->_needen      = $needen;
        $this->_firstLogin  = $_firstLogin;
    }

    /**
     * Authenticates a user.
     * The example implementation makes sure if the username and password
     * are both 'demo'.
     * In practical applications, this should be changed to authenticate
     * against some persistent user identity storage (e.g. database).
     * @return boolean whether authentication succeeds.
     */
    public function authenticate()
    {
        if(!User::mobileRegistered($this->username))
            $this->errorCode = self::ERROR_USERNAME_INVALID;
        else
        {
            $data = array(
                'account'     => $this->username,
                'password'    => md5($this->password),
                'ip'          => HttpLib::getRemoteIp(),
            );
            if (!$this->_needen)
                $data['password'] = $this->password;
            //$result = HttpLib::post(Yii::app()->params['scripts']['user_login'], $data);
            //$result = HttpLib::post('http://192.168.10.188:9101/cgtz-login/rest/login/mobile/do.json', $data);
            if($this->_firstLogin)
                $result = HttpLib::post('loginRest/mobile/do.json', $data);
            else
                $result = HttpLib::post('login/mobile/do.json', $data,'back_host2');
            if ($result)
            {
                if ($result->code != 0)
                {
                    $this->errorCode = $result->code;
                }
                else
                {
                    if ($result->result->status > 0)
                    {
                        $info = array(
                            'userId'    => $result->result->userId,
                            'token'     => $result->result->token,
                            'mobile'    => $result->result->mobileNumber,
                        );
                        $this->_id = $result->result->userId;
                        $this->_mobile = $result->result->mobileNumber;
                        $this->setState('info',$info);
                        unset($result);
                        $this->errorCode = self::ERROR_NONE;
                        Yii::app()->cache_php->del('Captcha.numErrorLeft'.$this->username);
                        Yii::app()->cache_php->del('captcha.'.$this->username);
                    }
                    else
                    {
                        $this->errorCode = self::ERROR_USERNAME_INVALID;
                    }
                }
            }
            else
            {
                $this->errorCode = self::ERROR_USERNAME_INVALID;
            }
        }
        return !$this->errorCode;
    }

    public function getId()
    {
        return $this->_id;
    }
    public function getMobile()
    {
        return $this->_mobile;
    }

}
